package Utils;

import android.content.Context;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import app.MyApplication;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitUtils {
    private static Retrofit mRetrofit = null;
    private static Context mContext;
    public final static String TOKEN = "token";
    public final static String SOURCE_VALUE = "android";
    public final static String SOURCE_KEY = "from";
    public final static String CONTENT_TYPR = "Content-Type";
    public final static String X_REQUESTED_WITH_KEY = "X-Requested-With";
    public final static String X_REQUESTED_WITH_VALUE = "XMLHttpRequest";
    public final static String APP_VERSION = "app_version";
    public final static String MOBILECODE = "mobileCode";
    public final static int CONNECT_TIME_OUT_SECOND = 10;
    //安全平台请求头
    public final static String AUTHORIZATION = "Authorization";
    public final static String AUTHORIZATIONMAIN = "Basic TEpZWVg6TEpZWVhAMTIzNA==";
    //外网地址
    public final static String WW = "https://www.st12306.cn/xproxy/yproxy/ljyyx/rest/app/";
    //测试地址
    public final static String CS = "http://10.136.72.210:9080/app-ljyyx/rest/app/";

    public final static String ZSNW = "http://10.136.3.88:7080/app-ljyyx/rest/app/";


    public static synchronized Retrofit getInstance() {
        if (mRetrofit == null) {
            mContext = MyApplication.getInstance();
            mRetrofit = new Retrofit.Builder().baseUrl(WW).client(genericClient())
                    .addConverterFactory(GsonConverterFactory.create()).build();
        }
        return mRetrofit;
    }


    public static OkHttpClient genericClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        setSSL(builder);
        OkHttpClient httpClient = builder.connectTimeout(CONNECT_TIME_OUT_SECOND, TimeUnit.SECONDS)
                .readTimeout(CONNECT_TIME_OUT_SECOND, TimeUnit.SECONDS)
                .writeTimeout(CONNECT_TIME_OUT_SECOND, TimeUnit.SECONDS)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request().newBuilder()
                                .addHeader(CONTENT_TYPR, "application/json;charset=UTF-8")
                                .addHeader(AUTHORIZATION,AUTHORIZATIONMAIN)
                                .build();

                        return chain.proceed(request);
                    }

                })
                .build();
        return httpClient;
    }

    private static OkHttpClient.Builder setSSL(OkHttpClient.Builder builder) {
        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[] {
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                        }

                        @Override
                        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                        }

                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[]{};
                        }
                    }
            };

            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            // Create an ssl socket factory with our all-trusting manager
            final javax.net.ssl.SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            builder.sslSocketFactory(sslSocketFactory);
            builder.hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });

            return builder;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
